{{if false}}<script>{{/if}}
{{$controller = $smarty.const.CCTRL}}
{{$token = $smarty.const.TOKEN}}
{{include file="../00_base/function.tpl"}}
function handleEvents(){
    try{
        
        myGrid.bind('cellendedit', function (event) {
            try{
                var args = event.args;
                var _column = args.datafield ,_row = args.rowindex,_value = args.value;
                var _data=myGrid.jqxGrid('getrowdata', _row);
                var _id=_data.ID;
                if(_id==undefined || _id==""){
                    return;
                }
                switch(_column){
                    case "Status":
                        if(_value=="true" || _value ==true) _value="Public";
                        else _value="Private";
                        updateCol(_column,_id,_value);
                        break;
                    default:
                    uiMessage("Unkown this cell. Maybe 'editable' of this colums is 'off'."); 
                }
            }catch(e){
                uiMessage(e.message);
                console.log("Error:"+e.message);
            }
        });
    }catch(e){tipMessage(e.message);}
}
function SaveItem(){
    try{
        if(isrunning)return;
        console.log("Save ↵ Call");
        var url="{{base_url()}}backend/{{$controller}}/save";
        var data={
            ID        :   $("#txtID").val(),
            VideoKey        :   $("#txtVideoKey").val(),
            Title           :   $("#txtTitle").val(),
            Alias           :   $("#txtAlias").val(),
            Thumbs          :   $("#txtThumbs").val(),
            Source          :   $("#txtLink").val(),
            Description     :   $("#txtDescription").val(),
            Tag             :   $("#txtTag").val()
        }
        //ShowNoticeDialogMessage($("<div />").text(Content).html());return;
        console.log(data);
        isrunning=true;
        myAjax(url,data,function(data){
            isrunning=false;
            if(data.result>=0){
                uiMessage(data.message)
                myjqxGrid.CancelEdit();
                myjqxGrid.Refresh();
            }else{
                ShowErrorDialogMessage(data.message);
            }
        });
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
function getYoutubeInfo(){
    if(isrunning)return;
    if( (!_FcheckFilled($("#txtLink").val())) && (!_FcheckFilled($("#txtVideoKey").val()))){
        uiMessage("Please enter Youtube link or Youtube Key.");
        return;
    }
    var url="{{base_url()}}backend/{{$controller}}/YoutubeInfo";
    //var url=baseurl+"admin-planners/video_climb/YoutubeInfo";
    var data={
        url:$("#txtLink").val(),
        key:$("#txtVideoKey").val()
    };
    myAjax(url,data,function(video){
        isrunning=false;
        try{
            $("#txtLink").val(video.watchURL);
            $("#txtTitle").val(video.title);
            $("#txtAlias").val(video.alias);
            $("#txtVideoKey").val(video.key);
            $("#txtAuthor").val(video.author);
            $("#txtThumbs").val(video.thumbnail);
            $("#txtDescription").val(video.description);
            $("#txtEmbel").val(video.embed);
            $("#txtLength").val(video.length);
            $("img.thumbs").attr("src",video.thumbnail);
        }catch(err){
            ShowErrorDialogMessage("Cant get video information.<br/> Please check your Youtube link or Youtube Key.");
        }
    });
}